1 /*
2 * Created by SharpDevelop.
3 * User: Janphil Garay
4 * Date: 16/02/2012
5 * Time: 12:52 AM
6 *
7 * To change this template use Tools | Options | Coding | Edit Standard Headers.
8 */
9 using System;
10 using System.Collections.Generic;
11 using System.Drawing;
12 using System.IO;
13 using System.Windows.Forms;
14
15 using Emgu.CV;
16 using Emgu.CV.CvEnum;
17 using Emgu.CV.Structure;
18
19 namespace FaceRecognition
20 {
21 /// <summary>
22 /// Description of frmEnrollment.
23 /// </summary>
24 public partial class frmEnrollment : Form
25 {
26 Image<Bgr, Byte> currentFrame;
27 Capture grabber;
28 HaarCascade face;
29 HaarCascade eye;
30 MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
31 Image<Gray, byte> result, TrainedFace = null;
32 Image<Gray, byte> gray = null;
33 List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
34 List<string> labels= new List<string>();
35 List<string> NamePersons = new List<string>();
36 int ContTrain, NumLabels, t;
37 string name, names = null;
38
39 public frmEnrollment()
40 {
41 InitializeComponent();
42
43 face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
44 eye = new HaarCascade("haarcascade_eye.xml");
45 try
46 {
47 string Labelsinfo = File.ReadAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt");
48 string[] Labels = Labelsinfo.Split('%');
49 NumLabels = Convert.ToInt16(Labels[0]);
50 ContTrain = NumLabels;
51 string LoadFaces;
52
53 for (int tf = 1; tf < NumLabels+1; tf++)
54 {
55 LoadFaces = "face" + tf + ".bmp";
56 trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/TrainedFaces/" + LoadFaces));
57 labels.Add(Labels[tf]);
58 }
59
60 }
61 catch(Exception e)
62 {
63 MessageBox.Show("Nothing in binary database, please add at least a face", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
64 }
65
66 }
67
68 void Button2Click(object sender, EventArgs e)
69 {
70 if (textBox1.Text.Equals(""))
71 {
72 MessageBox.Show("Please Enter Name before saving....");
73 return;
74 }
75 else
76 {
77 try
78 {
79
80 ContTrain = ContTrain + 1;
81
82
83 gray = grabber.QueryGrayFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
84
85
86 MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
87 face,
88 1.2,
89 10,
90 Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
91 new Size(20, 20));
92
93
94 foreach (MCvAvgComp f in facesDetected[0])
95 {
96 TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();
97 break;
98 }
99
100 TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
101 trainingImages.Add(TrainedFace);
102 labels.Add(textBox1.Text);
103
104
105 imageBox1.Image = TrainedFace;
106
107 File.WriteAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", trainingImages.ToArray().Length.ToString() + "%");
108
109 for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
110 {
111 trainingImages.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp");
112 File.AppendAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", labels.ToArray()[i - 1] + "%");
113 }
114
115 MessageBox.Show(textBox1.Text + "´s face detected and added :)", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
116 }
117 catch
118 {
119 MessageBox.Show("Enable the face detection first", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
120 }
121 }
122
123 }
124
125 void FrmEnrollmentLoad(object sender, EventArgs e)
126 {
127 grabber = new Capture();
128 grabber.QueryFrame();
129 Application.Idle += new EventHandler(FrameGrabber);
130 }
131 void FrameGrabber(object sender, EventArgs e)
132 {
133
134 NamePersons.Add("");
135
136
137 currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
138
139 gray = currentFrame.Convert<Gray, Byte>();
140
141 MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
142 face,
143 1.2,
144 10,
145 Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
146 new Size(20, 20));
147
148 foreach (MCvAvgComp f in facesDetected[0])
149 {
150 t = t + 1;
151 result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
152 currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
153
154
155 if (trainingImages.ToArray().Length != 0)
156 {
157 MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
158
159 EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
160 trainingImages.ToArray(),
161 labels.ToArray(),
162 5000,
163 ref termCrit);
164
165 name = recognizer.Recognize(result);
166
167 currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));
168
169 }
170
171 NamePersons[t-1] = name;
172 NamePersons.Add("");
173 /*
174 gray.ROI = f.rect;
175 MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
176 eye,
177 1.1,
178 10,
179 Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
180 new Size(20, 20));
181 gray.ROI = Rectangle.Empty;
182
183 foreach (MCvAvgComp ey in eyesDetected[0])
184 {
185 Rectangle eyeRect = ey.rect;
186 eyeRect.Offset(f.rect.X, f.rect.Y);
187 currentFrame.Draw(eyeRect, new Bgr(Color.Blue), 2);
188 }
189 */
190
191 }
192 t = 0;
193
194 for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
195 {
196 names = names + NamePersons[nnn] + ", ";
197 }
198
199 imageBoxFrameGrabber.Image = currentFrame;
200
201 NamePersons.Clear();
202
203 }
204
205 }
206 }